package com.lovo.frame;

import com.lovo.pojo.UserDO;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * GUI版本的ATM
 */
public class ATM extends JFrame {
    private ArrayList<UserDO> users;
    private Image bgImg = Util.getImage("/images/background.jpg");
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    public static final String PATH = "users.dat";
    // 登录成功的用户对象
    private UserDO loginUser;
    public ATM(){
//        init();
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });
        this.setLayout(cardLayout);

        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(searchPanel,"search");
        this.add(getPanel,"get");
        this.add(transferPanel,"transfer");

        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    // 初始化用户集合
//    public void init(){
//        users = (ArrayList)FileUtil.readObject(PATH);
//        if(users == null){
//            users = new ArrayList<>();
//            users.add(new User("aaaaaa","111111",10000));
//            users.add(new User("bbbbbb","222222",20000));
//            users.add(new User("cccccc","333333",30000));
//            users.add(new User("dddddd","444444",40000));
//            users.add(new User("eeeeee","555555",50000));
//            FileUtil.writeObject(PATH,users);
//        }
//
//    }


    public void switchPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }

    public ArrayList<UserDO> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<UserDO> users) {
        this.users = users;
    }

    public UserDO getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(UserDO loginUser) {
        this.loginUser = loginUser;
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public TransferPanel getTransferPanel() {
        return transferPanel;
    }

    public void setTransferPanel(TransferPanel transferPanel) {
        this.transferPanel = transferPanel;
    }

    public GetPanel getGetPanel() {
        return getPanel;
    }

    public void setGetPanel(GetPanel getPanel) {
        this.getPanel = getPanel;
    }

    public static void main(String[] args) {
        new ATM();
    }

}
